package com.kaka.bus.engine.home.info.card;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kaka.bus.R;
import com.kaka.bus.adapter.CardListAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.CardBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.component.DaggerLoanComponent;
import com.kaka.bus.engine.home.main.h5.H5Activity;
import com.kaka.bus.util.ToastUtil;
import com.kaka.bus.util.U;
import com.kaka.bus.view.MutipleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CardListActivity extends BaseActivity<CardListPresenter> implements CardListContract.View {
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;

    @BindView(R.id.cardlist_recy)
    RecyclerView cardlist_recy;
    @BindView(R.id.cardlist_smart)
    SmartRefreshLayout cardlist_smart;
    @BindView(R.id.cardlist_muti)
    MutipleLayout cardlist_muti;

    private int mPage = 1;
    private int mPageSize = 20;
    private CardListAdapter mCardListAdapter;
    private List<CardBean> mDatas = new ArrayList<>();
    private Intent mIntent;


    @Override
    protected void initView() {
        super.initView();
        mIntent = new Intent();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("我的卡包");
        cardlist_recy.setLayoutManager(new LinearLayoutManager(this));
        mCardListAdapter = new CardListAdapter(R.layout.layout_jdcard, mDatas);
        cardlist_recy.setAdapter(mCardListAdapter);
        mCardListAdapter.bindToRecyclerView(cardlist_recy);
        cardlist_muti.setEmptyText("您当前暂无记录!");
        cardlist_muti.setEmptyImage(R.drawable.empty_view_kongtai);
        cardlist_muti.setStatus(MutipleLayout.Loading);

    }


    @Override
    public void onError() {
        mPage--;
        cardlist_smart.finishLoadmore(0);
        cardlist_muti.setStatus(MutipleLayout.Error);
    }

    @Override
    protected void initData() {
        super.initData();
        getData();
    }


    @Override
    public void onResultStart(HttpWrapper<List<CardBean>> data) {
        cardlist_smart.finishRefresh(0);
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            if (data.data != null && data.data.size() > 0) {
                mCardListAdapter.addData(data.data);
                cardlist_muti.setStatus(MutipleLayout.Success);
            } else {
                cardlist_muti.setStatus(MutipleLayout.Empty);
            }
        } else if (Constant.ERROR_CODE.equals(data.getCode())) {
            showMessage(data.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    public void onResultLoadMore(HttpWrapper<List<CardBean>> o) {
        cardlist_smart.finishLoadmore(0);
        if (Constant.SUCCESS_CODE.equals(o.getCode())) {
            if (o.data != null && o.data.size() > 0) {
                mCardListAdapter.addData(o.data);
            } else {
                showMessage("没有更多数据了");
                mPage--;
            }
        } else if (Constant.ERROR_CODE.equals(o.getCode())) {
            showMessage(o.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(o.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getData();
    }

    @Override
    public void refresh(HttpWrapper<List<CardBean>> data) {
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            getData();
        } else {
            ToastUtil.showShort(this, "取消失败！");
        }
    }

    public void getData() {
        mPage = 1;
        mDatas.clear();
        mPresenter.initData(getUserId(), getToken(), 1, mPageSize);
    }

    @Override
    protected void initListener() {
        cardlist_smart.setOnRefreshListener(refreshlayout -> {
            getData();
        });

        cardlist_smart.setOnLoadmoreListener(refreshlayout -> {
            mPresenter.loadMore(getUserId(), getToken(), ++mPage, mPageSize);
        });

//        mCardListAdapter.setOnItemClickListener((adapter, view, position) -> {
//            String number = mDatas.get(position).getBorrStatus().trim();
//            if (number.equals("BS004") || number.equals("BS005") || number.equals("BS006") || number.equals("BS014") || number.equals("BS010")) {
//                Intent intent = new Intent(this, BorrowDeteilActivity.class);
//                intent.putExtra(BorrowDeteilActivity.BORROWID, mDatas.get(position).getId());
//                startActivity(intent);
//            }
//        });
        mCardListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_carddtail) {
                    mIntent.setClass(CardListActivity.this, H5Activity.class);
                    mIntent.putExtra(H5Activity.URL, mDatas.get(position).getDetailUrl());
                    startActivity(mIntent);
                }
            }
        });

        cardlist_muti.setOnReloadListener(v -> {
            getData();
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_card_list;
    }

    @Override
    protected void initInject() {
        DaggerLoanComponent.builder().activityComponent(getActivityComponent()).build().inject(this);
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }


}
